package com.mall.order.services;


import com.alibaba.fastjson.JSON;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.StockMapper;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Component
public class RocketConsumer {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    StockMapper stockMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    DefaultMQPushConsumer defaultMQPushConsumer;

    //如果时间长没有支付订单，则取消订单
    //因为本类是spring组件，开所以起应用就加载
	@PostConstruct
	public void init(){

		defaultMQPushConsumer = new DefaultMQPushConsumer("consumer_group");

		defaultMQPushConsumer.setNamesrvAddr("localhost:9876");

		try {
			defaultMQPushConsumer.subscribe("order","*");
		} catch (MQClientException e) {
			e.printStackTrace();
		}

		defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
			@Override
			public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {

				MessageExt messageExt = msgs.get(0);
				byte[] body = messageExt.getBody();

				String bodyStr = new String(body);
				Map<String,Object> map = JSON.parseObject(bodyStr, Map.class);

				String orderId = (String) map.get("order");

				// 处理相应的业务逻辑
				Order order = orderMapper.selectByPrimaryKey(orderId);
				if (order.getStatus()==0){

					//将订单取消
					orderMapper.updateByStatus(7,orderId);

					//查询orderid对应的orderItem表
					Example example = new Example(OrderItem.class);
					Example.Criteria criteria = example.createCriteria();
					criteria.andEqualTo("orderId", order.getOrderId());
					List<OrderItem> orderItems = orderItemMapper.selectByExample(example);

					//修改库存
					for (OrderItem orderItem : orderItems) {
						Stock stock = stockMapper.selectByPrimaryKey(orderItem.getItemId());
						Long stockCount = stock.getStockCount();
						stockCount += orderItem.getNum();
						Integer lockCount = stock.getLockCount();
						lockCount -=orderItem.getNum();
						stockMapper.updateStockSetStockCAndnewNum(stockCount,lockCount.longValue(),orderItem.getItemId());
					}
				}
				return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
			}
		});
		try {
			defaultMQPushConsumer.start();
		} catch (MQClientException e) {
			e.printStackTrace();
		}
	}
}
